package com.da.javatest.design.schema.singleton;

public class SingletonTest06 {
    private static volatile SingletonTest06 instance;

    private SingletonTest06() {}

    //提供一个静态的公有方法，加入双重检查代码，解决线程安全问题, 同时解决懒加载问题
    //同时保证了效率, 推荐使用
    public static synchronized SingletonTest06 getInstance() {
        if (instance == null) {
            synchronized (SingletonTest06.class) {
                if (instance == null) {
                    instance = new SingletonTest06();
                }
            }

        }
        return instance;
    }

    public static void main(String[] args) {
        System.out.println("双重检查");
        SingletonTest06 instance = SingletonTest06.getInstance();
        SingletonTest06 instance2 = SingletonTest06.getInstance();
        System.out.println(instance == instance2); // true
        System.out.println("instance.hashCode=" + instance.hashCode());
        System.out.println("instance2.hashCode=" + instance2.hashCode());
    }
}
